﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Security;

public partial class Admin_NewsList : System.Web.UI.Page
{
    int m_ColID = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string sql = "SELECT ID, Name FROM Category";
            DataTable dt = WFNetLib.ADO.SQLServerOP.DataTableSQL(sql);
            foreach (DataRow dr1 in dt.Rows)
            {
                ListItem li = new ListItem(dr1["Name"].ToString(), dr1["ID"].ToString());
                DropDownList1.Items.Add(li);
            }
            DropDownList1.Items.Insert(0,new ListItem("栏目名称","0"));
            DropDownList1.SelectedIndex = 0;
            BindDisplayList();

        }
    }
    private void BindDisplayList()
    {
        string sql;
        if (DropDownList1.SelectedIndex == 0)
            sql = "select * from News ORDER BY [Top] DESC, EditDate DESC";
        else
            sql = "select * from News where Category='" + DropDownList1.SelectedValue + "' ORDER BY [Top] DESC, EditDate DESC";
        DataTable tb = WFNetLib.ADO.SQLServerOP.DataTableSQL(sql);
        GridView1.Columns[m_ColID].Visible = true;
        GridViewOperation.BindPagingGridView(GridView1, tb.DefaultView, 0);
        GridView1.Columns[m_ColID].Visible = false;
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            GridViewOperation.AddConfirmGridView(e, 6, "确定要删除么?删除后无法回复!");
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "New":
                Response.Redirect("~/Admin/NewsEdit.aspx");
                break;
            case "Select":
                NewsData ind = new NewsData();
                ind.ID = new Guid(GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[m_ColID].Text);
                string str = ((DataBoundLiteralControl)GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[2].Controls[0]).Text;
                if (str.IndexOf("<img src='img/top.gif' />") != -1)
                    ind.Top = true;
                else
                    ind.Top = false;
                NewsDataDBOption.SetTop(ind);
                Response.Redirect("~/Admin/NewsList.aspx");
                break;

        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Guid ID = new Guid(GridView1.Rows[e.RowIndex].Cells[m_ColID].Text);
        NewsData imd = new NewsData();
        imd.ID = ID;
        NewsDataDBOption.Delete(imd);
        divStatusSuccess.Visible = true;
        BindDisplayList();
        //ScriptManager.RegisterStartupScript(Master.FindControl("UpdatePanel1"), this.GetType(), "AddNewsOk", "alert('删除成功!'); location.href='FlashManage.aspx'", true);
        //Response.Redirect("~/Admin/ImgNewsEdit.aspx");
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        Response.Redirect("~/Admin/NewsEdit.aspx?id=" + GridView1.Rows[e.NewEditIndex].Cells[m_ColID].Text);
    }
    public string GetTopImage(object dataItem)
    {
        Boolean IsTop = Convert.ToBoolean(DataBinder.Eval(dataItem, "Top").ToString());
        if (IsTop)
            return "<img src='img/top.gif' />";
        else
            return "";
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDisplayList();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Admin/NewsEdit.aspx");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();
        Response.Redirect("~/login.aspx");
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();
        Response.Redirect("~/login.aspx");
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Admin/SearchList.aspx?search=" + Server.UrlEncode(TextBox1.Text));
    }
}